﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Electure.DAL;

namespace Electure.Admin
{
    public partial class EditScienceDepartment : System.Web.UI.Page
    {
        int? Id
        {
            get
            {
                int? res = new int?();
                int id;
                if (int.TryParse(Request.QueryString["id"], out id))
                    res = id;
                return res;
            }
        }

        public static Dictionary<ScienceDepartmentType, string> ScienceDepartmentDictionary = new Dictionary<ScienceDepartmentType, string>() 
        { 
            {ScienceDepartmentType.Institute, "Інститут"},
            {ScienceDepartmentType.Faculty, "Факультет"},
            {ScienceDepartmentType.Department, "Навчально-науковий підрозділ"}
        };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            foreach (var item in ScienceDepartmentDictionary)
            {
                ddlScDepType.Items.Add(new ListItem() { Value = ((int)item.Key).ToString(), Text = item.Value });
            }

            var items = ddlScDepType.Items;
            if (Id.HasValue)
            {
                using(var rep = RepositoryFactory.GetScienceDepartmentRepository())
                {
                    var scDep = rep.GetById(Id.Value);
                    tbName.Text = scDep.Name;
                    ddlScDepType.SelectedValue = ((int)scDep.Type).ToString();
                }
            }
        }

        protected void button_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Save")
            {
                int id;
                using (var rep = RepositoryFactory.GetScienceDepartmentRepository())
                {
                    var instance = new ScienceDepartment()
                        {
                            Name = tbName.Text,
                            Type = ScienceDepartmentDictionary
                                .Where(item => ((int)item.Key).ToString() == ddlScDepType.SelectedValue)
                                .First()
                                .Key
                        };
                    if (Id.HasValue)
                    {
                        instance.ID = Id.Value;
                        if (rep.Update(instance))
                            id = Id.Value;
                        else
                            id = -1;
                    }
                    else
                    {
                        id = rep.Create(instance);
                    }
                }
                Response.Redirect(string.Format("EditScienceDepartment.aspx?id={0}", id));
            }
            else if (e.CommandName == "Cancel")
            {
                Response.Redirect("ScienceDepartmentList.aspx");
            }
        }
    }
}